﻿/*
 *author:keily ,department 2,wuda geo
 *create date:2013-1-29
 *description:ftp访问帮助类
 */
using System;
using System.Text;
using System.Net;
using System.IO;

namespace GeoWeb.Common
{
    /// <summary>
    /// ftp访问帮助类
    /// </summary>
    public class FtpHelper
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="uri">web request的url</param>
        public FtpHelper(Uri uri)
        {
            if (uri == null)
                throw new ArgumentNullException("uri");

            _ftpWebRequest = (System.Net.FtpWebRequest)WebRequest.Create(uri);
        }

        private FtpWebRequest _ftpWebRequest;
        /// <summary>
        /// 设置WebRequest的附加属性
        /// </summary>
        public FtpWebRequest FtpWebRequest
        {
            get { return _ftpWebRequest; }
        }

        private int _bufferLength = 2048;
        public int BufferLength
        {
            get { return _bufferLength; }
            set { _bufferLength = value; }
        }
        /// <summary>
        /// 加密文件上传
        /// </summary>
        /// <param name="data"></param>
        /// <param name="password"></param>
        /// <param name="salt"></param>
        /// <returns></returns>
        [Obsolete("无法使用")]
        public string EncryptAndUpload(byte[] data, string password, byte[] salt)
        {
            //return Upload(Encrypt(data, password, salt));
            return null;
        }
        /// <summary>
        /// 上传文件,需包含文件的完整url路径
        /// </summary>
        /// <param name="data">文件数组</param>
        /// <returns></returns>
        public string Upload(byte[] data)
        {

            _ftpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;

            byte[] buffer = new byte[_bufferLength];
            int contentLength;

            _ftpWebRequest.ContentLength = data.Length;

            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(data);

            System.IO.Stream stream = _ftpWebRequest.GetRequestStream();
            contentLength = memoryStream.Read(buffer, 0, _bufferLength);

            while (contentLength != 0)
            {
                stream.Write(buffer, 0, contentLength);
                contentLength = memoryStream.Read(buffer, 0, _bufferLength);
            }

            stream.Close();
            memoryStream.Close();

            return SendFtpWebRequest(_ftpWebRequest);
        }
        /// <summary>
        /// 下载文件,需包含文件的完整url路径
        /// </summary>
        /// <returns>文件数组</returns>
        public byte[] Download()
        {
            _ftpWebRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;

            byte[] buffer = new byte[_bufferLength];
            int contentLength = 0;
            MemoryStream ms = new MemoryStream();
            using (FtpWebResponse response = (FtpWebResponse)_ftpWebRequest.GetResponse())
            {
                using (Stream reader = response.GetResponseStream())
                {
                    contentLength = reader.Read(buffer, 0, buffer.Length);
                    while (contentLength != 0)
                    {
                        ms.Write(buffer, 0, contentLength);
                        contentLength = reader.Read(buffer, 0, buffer.Length);
                    }
                    reader.Close();
                }
                response.Close();
            }
            return ms.ToArray();
        }
        /// <summary>
        /// 获取下载文件的内容
        /// </summary>
        /// <returns></returns>
        public string GetFileContents()
        {
            byte[] data = Download();
            string contents;
            using (System.IO.StreamReader reader = new System.IO.StreamReader(new System.IO.MemoryStream(data)))
            {
                contents = reader.ReadToEnd();
            }

            return contents;
        }
        [Obsolete("无法使用")]
        public string DecryptFileContents(string password, byte[] salt)
        {
            //byte[] data = Download();
            //byte[] clearBytes = Decrypt(data, password, salt);
            //string contents;
            //using (System.IO.StreamReader reader = new System.IO.StreamReader(new System.IO.MemoryStream(clearBytes)))
            //{
            //    contents = reader.ReadToEnd();
            //}

            //return contents;
            return null;
        }
        /// <summary>
        /// 获取ftp服务器的文件流
        /// </summary>
        /// <param name="ftpWebRequest"></param>
        /// <returns></returns>
        public string SendFtpWebRequest(FtpWebRequest ftpWebRequest)
        {
            if (ftpWebRequest == null)
                throw new ArgumentNullException("ftpWebRequest");

            using (FtpWebResponse response = (FtpWebResponse)ftpWebRequest.GetResponse())
            {
                try
                {
                    using (Stream dataStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(dataStream))
                        {
                            return reader.ReadToEnd();
                        }
                    }
                }
                finally
                {
                    response.Close();
                }
            }
        }
    }
}
